﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using Forest.Core;
using Forest.Core.Events;
using Forest.Core.Caching;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.CoverLetters {
    public partial class CoverLetterService : ICoverLetterService {
        #region Constants
        private const string COVERLETTER_BY_ID_KEY = "coverletter.id-{0}";
        private const string COVERLETTER_PATTERN_KEY = "coverletter.";
        #endregion

        #region Fields
        private readonly IRepository<CoverLetter> _coverletterRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenerateNumberService _generateNumberService;

        #endregion

        #region Ctor
        public CoverLetterService(IRepository<CoverLetter> coverletterRepository, ICacheManager cacheManager, IEventPublisher eventPublisher, IGenerateNumberService generateNumberService) {
            _coverletterRepository = coverletterRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
            _generateNumberService = generateNumberService;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteCoverLetter(CoverLetter coverletter) {
            if (coverletter == null)
                throw new ArgumentNullException("CoverLetter");

            _coverletterRepository.Delete(coverletter);

            _cacheManager.RemoveByPattern(COVERLETTER_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(coverletter);

        }

        public CoverLetter GetCoverLetterById(Guid coverletterId) {
            if (coverletterId == Guid.Empty)
                return null;

            string key = string.Format(COVERLETTER_BY_ID_KEY, coverletterId);

            return _cacheManager.Get(key, () => {
                var cl = _coverletterRepository.GetById(coverletterId);
                return cl;
            });
        }

        public virtual IPagedList<CoverLetter> GetAllCoverLetters(int pageIndex, int pageSize) {
            var query = _coverletterRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<CoverLetter>(query, pageIndex, pageSize);
            return items;
        }

        public virtual IList<CoverLetter> GetCoverLetters(Guid jobseekerId) {
            var query = _coverletterRepository.Table;
            query = query.Where(c => c.JobseekerId == jobseekerId).OrderByDescending(c => c.LastUpdated);

            return query.ToList();
        }

        public virtual void InsertCoverLetter(CoverLetter coverletter) {
            if (coverletter == null)
                throw new ArgumentNullException("CoverLetter");

            _coverletterRepository.Insert(coverletter);

            _cacheManager.RemoveByPattern(COVERLETTER_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(coverletter);
        }

        public virtual void UpdateCoverLetter(CoverLetter coverletter) {
            if (coverletter == null)
                throw new ArgumentNullException("CoverLetter");
            _coverletterRepository.Update(coverletter);

            _cacheManager.RemoveByPattern(COVERLETTER_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(coverletter);
        }
        #endregion

        #region Extend Methods

        #endregion

        public Guid CreateId {
            get {
                return _generateNumberService.GenerateId<CoverLetter>(_coverletterRepository, GenerateType.CoverLetter);
            }
        }
    }
}
